package com.szxb.facebus;

import android.app.Application;
import android.os.IInterface;

import com.lilei.tool.tool.IToolInterface;
import com.szxb.facebus.db.manager.DBCore;
import com.szxb.facebus.intrefaces.OnIInterface;
import com.szxb.facebus.manager.PosManager;
import com.szxb.facebus.task.bindservice.BindService;
import com.szxb.facebus.task.work.WorkThread;
import com.szxb.facebus.util.basics.Constant;
import com.szxb.facebus.util.log.MLog;
import com.szxb.facebus.util.schedule.ThreadFactory;
import com.szxb.facebus.util.sound.SoundPoolUtil;
import com.szxb.mlog.AndroidLogAdapter;
import com.szxb.mlog.CsvFormatStrategy;
import com.szxb.mlog.DiskLogAdapter;
import com.szxb.mlog.FormatStrategy;
import com.szxb.mlog.PrettyFormatStrategy;
import com.szxb.mlog.SLog;
import com.szxb.q8server.MyTool;
import com.tencent.wlxsdk.WlxSdk;
import com.yanzhenjie.nohttp.InitializationConfig;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.OkHttpNetworkExecutor;

import java.util.concurrent.TimeUnit;

/**
 * 作者：Tangren on 2018-12-07
 * 包名：com.szxb.facebus
 * 邮箱：996489865@qq.com
 * TODO:一句话描述
 */

public class BusApp extends Application implements OnIInterface {

    private volatile static BusApp instance;
    private static PosManager posManager;
    private IToolInterface mIToolInterface;
    private MyTool mMyTool;


    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        MLog.setDebug(true);
        DBCore.init(this);
        posManager = new PosManager();
        posManager.loadParams();
        WlxSdk.initSDK(getApplicationContext());
        NoHttp.initialize(InitializationConfig.newBuilder(this)
                .networkExecutor(new OkHttpNetworkExecutor())
                .connectionTimeout(10 * 1000)
                .readTimeout(10 * 1000)
                .build());
        Logger.setDebug(true);
        Logger.setTag("network");
        initLog();
        SoundPoolUtil.init(this);
        BindService bindService = new BindService(this);
        bindService.setOnIInterface(this);

        ThreadFactory.getScheduledPool().executeCycle(new WorkThread(Constant.UPDATE_LINE_FACE_LIST), 20, 60, Constant.UPDATE_LINE_FACE_LIST, TimeUnit.SECONDS);
//        ThreadFactory.getScheduledPool().executeCycle(new WorkThread(Constant.UPDATE_LINE_BLACK_FACE_LIST), 20, 60, Constant.UPDATE_LINE_BLACK_FACE_LIST, TimeUnit.SECONDS);
    }


    public static BusApp getInstance() {
        return instance;
    }

    public static PosManager getPosManager() {
        if (posManager == null) {
            posManager = new PosManager();
            posManager.loadParams();
        }
        return posManager;
    }


    private void initLog() {
        FormatStrategy format = PrettyFormatStrategy.newBuilder()
                .tag("szface")
                .showThreadInfo(false)
                .methodCount(1)
                .build();
        SLog.addLogAdapter(new AndroidLogAdapter(format));
        FormatStrategy formatStrategy = CsvFormatStrategy.newBuilder()
                .tag("szface")
                .fileName("测试")
                .build();
        SLog.addLogAdapter(new DiskLogAdapter(formatStrategy));
    }


    public IToolInterface getmService() {
        return mIToolInterface;
    }

    public MyTool getMyTool() {
        return mMyTool;
    }

    @Override
    public void setIInterface(IInterface mIInterface) {
        if (mIInterface instanceof IToolInterface) {
            mIToolInterface = (IToolInterface) mIInterface;
        } else if (mIInterface instanceof MyTool) {
            mMyTool = (MyTool) mIInterface;
        }
    }
}
